<?php
//by willson 105328468@qq.com 2014/10/11
//index page interface

class withdraw extends core {
	public $withdraw_mod;
	public function __construct() {
		parent::__construct();
		$this->withdraw_mod = m("withdraw");
	}

	//申请提现
	public function apply() {
		$withdraw_money = (isset($_POST['withdraw_money']) && floatval($_POST['withdraw_money'])) ? floatval($_POST['withdraw_money']) : 0;
		$name = (isset($_POST['name']) && trim($_POST['name'])) ? trim($_POST['name']) : '';
		$bank_card = (isset($_POST['bank_card']) && trim($_POST['bank_card'])) ? trim($_POST['bank_card']) : '';
		$bank_name = (isset($_POST['bank_name']) && trim($_POST['bank_name'])) ? trim($_POST['bank_name']) : '';
		$bank_branch = (isset($_POST['bank_branch']) && trim($_POST['bank_branch'])) ? trim($_POST['bank_branch']) : '';
		$alipay = (isset($_POST['alipay']) && trim($_POST['alipay'])) ? trim($_POST['alipay']) : '';
		
		$sql = "SELECT commission FROM jf_member WHERE id='{$this->member_id}'";
		$commission = floatval($this->db->get_var($sql));
		if ($commission<$withdraw_money) {
			error("需要提现的金额有误");
		}
		$sql = "INSERT INTO jf_withdraw(member_id, withdraw_money, name, bank_card, 
			bank_name, bank_branch, add_time, status, ip) VALUES(
			'{$this->member_id}', '{$withdraw_money}', '{$name}', '{$bank_card}', 
			'{$bank_name}', '{$bank_branch}', '".time()."', '0', '{$this->ip}')";
		$this->db->query($sql);
		$commission -= $withdraw_money;
		$sql = "UPDATE jf_member SET commission='{$commission}' WHERE id='{$this->member_id}'";
		$this->db->query($sql);
		$_SESSION["member"]->commission = $commission;
		success("ok");
	}

	//申请提现历史
	public function history() {
		$offset = (isset($_GET['offset']) && trim($_GET['offset'])) ? intval($_GET['offset']) : 0;
		$pagesize = (isset($_GET['pagesize']) && trim($_GET['pagesize'])) ? intval($_GET['pagesize']) : 15;				
		$sql = "SELECT withdraw_money, add_time, withdraw_memo as memo, status FROM jf_withdraw 
			WHERE member_id='{$this->member_id}' ORDER BY id DESC LIMIT {$offset}, {$pagesize}";
		$results = $this->db->get_results($sql);
		if ($results) {
			foreach ($results as $g) {
				$g->status_name = $this->withdraw_mod->status_name($g->status);
				$g->add_time = date("Y-m-d", $g->add_time);
				$g->title = '申请提现';
			}
		}
		success($results);
	}

}
